const fs = require("fs");
var SSH = require("simple-ssh");

/**
let ips = [
  "10.224.72.4",
  "10.224.72.5",
  "10.224.72.6",
  "10.224.72.7",
  "10.224.72.8",
  "10.224.72.9",
  "10.224.72.10",
  "10.224.72.11",
  "10.224.72.12",
  "10.224.72.13",
  "10.224.72.14",
  "10.224.72.15",
  "10.224.72.16",
  "10.224.72.17",
  "10.224.72.18",
  "10.224.72.19",
  "10.224.72.20",
  "10.224.72.21",
  "10.224.72.22",
  "10.224.72.23",
  "10.224.72.24",
  "10.224.72.25",
  "10.224.72.26",
  "10.224.72.27",
  "10.224.72.28",
  "10.224.72.29",
  "10.224.72.30",
  "10.224.72.31",
  "10.224.72.32",
  "10.224.72.33",
  "10.224.72.34",
  "10.224.72.35",
  "10.224.72.36",
  "10.224.72.37",
  "10.224.72.38",
  "10.224.72.39",
  "10.224.72.40",
  "10.224.72.41",
  "10.224.72.42",
  "10.224.72.43",
  "10.224.72.44",
  "10.224.72.45",
  "10.224.72.46",
  "10.224.72.47",
  "10.224.72.48",
  "10.224.72.49",
  "10.224.72.50",
  "10.224.72.51",
  "10.224.72.52",
  "10.224.72.53",
  "10.224.72.54",
  "10.224.72.55",
  "10.224.72.56",
  "10.224.72.2",
  "10.224.72.3",
  "10.224.72.248",
  "10.224.72.247",
  "10.224.72.246",
  "10.224.72.245",
  "10.224.72.244",
  "10.224.72.243",
  "10.224.72.242",
  "10.224.72.241",
  "10.224.72.240",
  "10.224.72.239",
  "10.224.72.238",
  "10.224.72.237",
  "10.224.72.236",
  "10.224.72.235",
  "10.224.72.234",
  "10.224.72.233",
  "10.224.72.232",
  "10.224.72.231",
  "10.224.72.230",
  "10.224.72.229",
  "10.224.72.228",
  "10.224.72.227",
  "10.224.72.226",
  "10.224.72.22",
  "10.224.72.224",
  "10.224.73.7",
  "10.224.73.8",
  "10.224.73.9",
  "10.224.74.7",
  "10.224.74.8",
  "10.224.74.9",
  "10.224.74.11",
  "10.224.72.254",
];
*/

let ips = ["192.168.1.1", "192.168.1.3"];

let comms = ["scr dis", "display diag", "display version", "display device verbose", "display device manuinfo", "display boot-loader", "display cpu", "display cpu-usage history slot 1", "display memory", "display clock", "display ntp-service status", "display fan", "display environment", "display power-supply verbose", "display interface", "display interface brief", "display counters inbound interface", "display counters outbound interface", "display counters rate inbound interface", "display counters rate outbound interface", "display irf", "display irf topology", "display irf configuration", "display stp brief", "display logbuffer", "display link-aggregation summary", "display link-aggregation verbose", "display lldp nei list", "dis ip routing-table", "display ip routing-table summary", "display mac-address mac-move", "display alarm", "display current-configuration", "undo scr dis"];

let dataMap = {};

async function shell(ip) {
  let result = [];
  let ssh = new SSH({
    host: ip,
    port: 22,
    user: "ping",
    pass: "Changeme_123",
  });
  await new Promise((resolve, reject) => {
    for (let i = 0, len = comms.length; i < len; i++) {
      let allStdout = "";
      const command = comms[i];
      ssh.exec(command, {
        in: i === 1 ? "n" : null,
        out: function (stdout) {
          allStdout += stdout;
        },
        err: function (stderr) {
          console.error("命令 [%o] 执行失败, 原因: %o", command, stderr);
          reject(stderr);
        },
        exit: function (code) {
          if (code === 0) {
            console.log("命令 [%o] 执行成功, 结果: %o", command, allStdout);
            result.push({ idx: i, key: command, val: allStdout });
            if (i === len - 1) {
              dataMap[ssh.host] = result;
              resolve();
            }
          }
        },
      });
    }
    ssh.start({
      success: () => {
        console.log("exec.start.success");
      },
      fail: (err) => {
        console.error("exec.start.failure", err);
      },
    });
  });
}

(async function () {
  for (let i = 0, len = ips.length; i < len; i++) {
    await shell(ips[i]);
  }
  const dirPath = `${__dirname}/json/`;
  if (!fs.existsSync(dirPath)) fs.mkdirSync(dirPath, { recursive: true });
  const filePath = `${dirPath}/${new Date().getTime()}.json`;
  fs.writeFileSync(filePath, JSON.stringify(Object.assign({}, dataMap), null, 2));
})();
